var schedule = require('node-schedule');
global.date = new Date(); 
const Lottery = require('./Lottery');
let lottery = new Lottery();
function formatDate(instanceDate){
  var year=instanceDate.getFullYear();
  var month=change(instanceDate.getMonth()+1);
  var day=change(instanceDate.getDate());
  var hour=change(instanceDate.getHours());
  var minute=change(instanceDate.getMinutes());
  var second=change(instanceDate.getSeconds());
  function change(t){
    if(t<10){
     return "0"+t;
    }else{
     return t;
    }
  }
  var time=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
  var issue=year+''+month+''+day+''+hour+''+minute+''+change(second/3);

  return {
  	time,
  	issue
  }
}


function scheduleRecurrenceRule(){

    var rule = new schedule.RecurrenceRule();
    // rule.dayOfWeek = 2;
    // rule.month = 3;
    // rule.dayOfMonth = 1;
    // rule.hour = 1;
    // rule.minute = 42;
    var i = -1;
    var arr = [];
    while(i++<20){arr.push(i*3)}
	rule.second =arr;
    console.log(arr);

    console.log(date);
    schedule.scheduleJob(rule, function(){
    	global.data=new Date()
       console.log(`期号：${formatDate(global.data).issue}时间：${formatDate(global.data).time}开奖结果：${lottery.getcode()}`);
       // global.date = new Date(); 
    });
   
}

scheduleRecurrenceRule();